<?php
class MerchantApp extends MemberbaseApp
{
	var $_merch_mod;
	
	function __construct(){
		$this->MerchantApp();
	}
	
	function MerchantApp(){
		parent::__construct();
		$this->_merch_mod=&m('merchant_fabric');
	}
	
	function index(){
		$user_info=$this->visitor->get();
		$page = $this->_get_page(10);
		$conditions=$this->_get_query_conditions(array(
				array(
						'field' => 'demand_sn',
						'equal' => 'like',
						'name'	 => 'demand_sn'
				)
		));
		if ($user_info['role']==USER_MERCHANDISER_ADMIN){
			$all_user=$this->child_user();
			$conditions.=" and user_id".db_create_in($all_user);
		}
		if ($user_info['role']==USER_MERCHANT){
			$conditions.=" and demand_status=2 and FIND_IN_SET({$user_info['user_id']},assign_merchant_id)";
		}elseif (in_array($user_info['role'], array(USER_MANAGER,USER_MERCHANDISER_ADMIN,USER_CIVILIAN))){
			$conditions.=" and demand_status!=3";
		}else{
			$conditions.=" and user_id={$user_info['user_id']} and demand_status!=3";
		}
		
		$order = empty($_GET['order']) ? 'demand_time DESC' : str_replace('-', ' ', trim($_GET['order']));
		if ($order != 'demand_time DESC' || $order != 'demand_time ASC') {
			$order .= ', demand_time DESC';
		}
		$merch_list=$this->_merch_mod->find(array(
			'conditions'=>'1=1 '.$conditions,
			'order'=>$order,
			'limit'=>$page['limit'],
			'count'=>true	
		));		
		if (!empty($merch_list)){
			$file_mod=&m('upload');
			foreach ($merch_list as $key=>$val){
				$files=$file_mod->get(array(
					'conditions'=>'file_type'.db_create_in(array(BELONGS_SAMPLE_REF_IMG,BELONGS_ORDER_REF_IMG)).' and key_id='.$val['md_id']
				));
				$merch_list[$key]['merch_img']=$files['file_path'];
			}
		}
		$this->_format_page($page);
		$this->assign('page_info', $page);
		$this->assign('merch_list',$merch_list);
		$this->assign('user',$user_info);
		$this->display('merchant.index.html');
		$this->_curlocal(Lang::get('demand_list'));
	}
	
	//待审批需求
	function fabric_approve(){
		$user_info=$this->visitor->get();
		$page = $this->_get_page(10);
		$conditions=$this->_get_query_conditions(array(
				array(
						'field' => 'demand_sn',
						'equal' => 'like',
						'name'	 => 'demand_sn'
				)
		));
		if ($user_info['role']==USER_MERCHANDISER_ADMIN){
			$all_user=$this->child_user();
			$conditions.=" and user_id".db_create_in($all_user);
		}
		if (in_array($user_info['role'], array(USER_MANAGER,USER_MERCHANDISER_ADMIN,USER_CIVILIAN))){
			$conditions.=" and demand_status=1";
		}
		
		$order = empty($_GET['order']) ? 'demand_time DESC' : str_replace('-', ' ', trim($_GET['order']));
		if ($order != 'demand_time DESC' || $order != 'demand_time ASC') {
			$order .= ', demand_time DESC';
		}
		$merch_list=$this->_merch_mod->find(array(
				'conditions'=>'1=1 '.$conditions,
				'order'=>$order,
				'limit'=>$page['limit'],
				'count'=>true
		));
		if (!empty($merch_list)){
			$file_mod=&m('upload');
			foreach ($merch_list as $key=>$val){
				$files=$file_mod->get(array(
						'conditions'=>'file_type'.db_create_in(array(BELONGS_SAMPLE_REF_IMG,BELONGS_ORDER_REF_IMG)).' and key_id='.$val['md_id']
				));
				$merch_list[$key]['merch_img']=$files['file_path'];
			}
		}
		$this->_format_page($page);
		$this->assign('page_info', $page);
		$this->assign('merch_list',$merch_list);
		$this->assign('user',$user_info);
		$this->display('merchant.index.html');
		$this->_curlocal(Lang::get('merchant_approve'));
	}
	
	//添加面料需求
	function merch_fabric(){
		$order_id=empty($_GET['order_id'])?0:intval($_GET['order_id']);
		$sample_id=isset($_GET['sample_id'])?$_GET['sample_id']:0;
		$order_mod=& m('order');
		$sample_mod=& m('sample');
		$user=$this->visitor->get();
		if (!$order_id && !$sample_id){
			$this->show_warning('order_not_exist');
			return;
		}
		if ($order_id){
			$merchs=$this->_merch_mod->get(array(
					'conditions'=>'order_id='.$order_id
			));
		}elseif ($sample_id){
			$merchs=$this->_merch_mod->get(array(
					'conditions'=>'sample_id='.$sample_id
			));
		}else{
			$merchs=array();
		}
		
		if (!empty($merchs)){
			header('location:index.php?app=merchant&act=show&md_id=' . $merchs['md_id']);
			return;
		}
		if (!IS_POST){
			$file_mod=&m('upload');
			if ($order_id){
				$order=$order_mod->get($order_id);
				//参考图片
// 				$order['ref_merch']=$file_mod->find(array(
// 						'conditions' => 'file_type = ' . BELONGS_ORDER_REF_IMG . ' and key_id='.DEFAULT_CUSTOMER_KEY_ID." and session_id='".session_id()."'",
// 						'order' => 'file_id ASC',
// 				));
// 				$order['recomm_merch']=$file_mod->find(array(
// 						'conditions' => 'file_type = ' . BELONGS_ORDER_RECOMM_COLOR . ' and key_id='.DEFAULT_CUSTOMER_KEY_ID." and session_id='".session_id()."'",
// 						'order' => 'file_id ASC',
// 				));
				$this->assign('merch',$order);
				$this->assign('belongs_ref_img',BELONGS_ORDER_REF_IMG);
				$this->assign('belongs_recomm_color',BELONGS_ORDER_RECOMM_COLOR);
			}elseif ($sample_id){
				$sample=$sample_mod->get($sample_id);
				//参考图片
				$sample['ref_merch']=$file_mod->find(array(
						'conditions' => 'file_type = ' . BELONGS_SAMPLE_REF_IMG . ' and key_id='.DEFAULT_CUSTOMER_KEY_ID." and session_id='".session_id()."'",
						'order' => 'file_id ASC',
				));
				$sample['recomm_merch']=$file_mod->find(array(
						'conditions' => 'file_type = ' . BELONGS_SAMPLE_RECOMM_COLOR . ' and key_id='.DEFAULT_CUSTOMER_KEY_ID." and session_id='".session_id()."'",
						'order' => 'file_id ASC',
				));
				$this->assign('merch',$sample);
				$this->assign('belongs_ref_img',BELONGS_SAMPLE_REF_IMG);
				$this->assign('belongs_recomm_color',BELONGS_SAMPLE_RECOMM_COLOR);
			}
			$this->assign('default_key_id',DEFAULT_CUSTOMER_KEY_ID);
			$this->assign('user',$user);
			$this->display('merchant.fabric.html');
		}else{
			$demand_descrption=trim($_POST['demand_descrption']);
			if (empty($demand_descrption)){
				$this->show_warning("请填写您的需求描述");
				return;
			}
			$data=array(
					'demand_descrption'=>$demand_descrption,
					'demand_time'=>gmtime(),
					'order_id'	=> isset($order_id)?$order_id:0,
					'sample_id'	=> isset($sample_id)?$sample_id:0,
					'user_id'	=>$user['user_id']
			);
			$md_id=$this->_merch_mod->add($data);
			if (strlen ( $md_id ) <= 6) {
				$k = 6 - strlen ( $md_id );
				for($i = 0; $i < $k; $i ++) {
					$len .= 0;
				}
				$demand_sn = 'MD' . $len . $md_id;
			}
			$this->_merch_mod->edit($md_id,array('demand_sn'=>$demand_sn));
			if ($this->_merch_mod->has_error()){
				$this->show_warning($this->_merch_mod->get_error());
				return;
			}
			$s_ref_img_id=isset($_POST['s_ref_img_id'])?$_POST['s_ref_img_id']:"";
			$file_mod =& m('upload');
			if(!empty($s_ref_img_id))
			{
				$file_ids=implode(",",$s_ref_img_id);
				$cust_files_info=$file_mod->find(array(
						"conditions"=>"file_id in({$file_ids}) AND file_type=".BELONGS_SAMPLE_REF_IMG." AND key_id=".DEFAULT_CUSTOMER_KEY_ID." and session_id='".session_id()."'"
								));
					
				foreach($cust_files_info as $item)
				{
					$file_path = str_replace(DEFAULT_CUSTOMER_KEY_ID, $md_id, $item['file_path']);
					if(!file_exists(dirname($file_path))) {
						mkdir(dirname($file_path), 0777);
					}
					rename($item['file_path'], $file_path);
					$file_mod->edit("file_id={$item['file_id']}",array(
							"file_path"=>$file_path,
							"key_id"=>$md_id,
							"session_id"=>session_id()
					));
				}
			}
			$this->show_message('添加成功,请分配对象');
		}
	}
	
	
	function view_email(){
		$md_id=empty($_GET['md_id'])?0:intval($_GET['md_id']);
		$user=$this->visitor->get();
		if (!$md_id){
			$this->show_warning('demand_no_exist');
			return;
		}
		$merchs=$this->_merch_mod->get($md_id);
		$member_mod=&m('member');
		if ($merchs['assign_merchant_id']){
			$merch_arr=array_unique(explode(',', $merchs['assign_merchant_id']));
			foreach($merch_arr as $item)
			{
				$member_info=$member_mod->get($item);
				if(!empty($member_info['email']))
				{
					$email_list[]=$member_info['email'];
				}
			}
			if (!empty($email_list)){
				$emails=implode(';', array_unique($email_list));
			}
			$this->assign('email_list',$emails);
		}
		$this->display('email.list.html');
	}
	
	function show(){
		$md_id=empty($_GET['md_id'])?0:intval($_GET['md_id']);
		$user=$this->visitor->get();
		if (!$md_id){
			$this->show_warning('demand_no_exist');
			return;
		}
		$order_mod=&m('order');
		$sample_mod=&m('sample');
		$merchs=$this->_merch_mod->get($md_id);
		if (!IS_POST){
			$file_mod=&m('upload');
			if ($merchs['order_id']){
				$orders=$order_mod->get(array('conditions'=>'order_id='.$merchs['order_id'],'fields'=>'order_sn'));
				$merchs['order_sn']=$orders['order_sn'];
				$this->assign('is_order',true);
				$merchs['ref_merch']=$file_mod->find(array(
						'conditions'=>'file_type='.BELONGS_ORDER_REF_IMG.' and key_id='.$md_id,
						'order'=>'file_id asc'
				));
				if ($user['role']==USER_MERCHANT){
					$merchs['recomm_merch']=$file_mod->find(array(
							'conditions'=>'file_type='.BELONGS_ORDER_RECOMM_COLOR.' and key_id='.$md_id.' and merch_id='.$user['user_id'],
							'order'=>'file_id asc'
					));
				}else{
					$merchs['recomm_merch']=$file_mod->find(array(
							'conditions'=>'file_type='.BELONGS_ORDER_RECOMM_COLOR.' and key_id='.$md_id,
							'order'=>'file_id asc'
					));
				}
				$this->assign('belongs_ref_img',BELONGS_ORDER_REF_IMG);
				$this->assign('belongs_recomm_color',BELONGS_ORDER_RECOMM_COLOR);
			}
			if ($merchs['sample_id']){
				$samples=$sample_mod->get(array('conditions'=>'sample_id='.$merchs['sample_id'],'fields'=>'sample_sn'));
				$merchs['sample_sn']=$samples['sample_sn'];
				$merchs['ref_merch']=$file_mod->find(array(
						'conditions'=>'file_type='.BELONGS_SAMPLE_REF_IMG.' and key_id='.$md_id,
						'order'=>'file_id asc'
				));
				if ($user['role']==USER_MERCHANT){
					$merchs['recomm_merch']=$file_mod->find(array(
							'conditions'=>'file_type='.BELONGS_SAMPLE_RECOMM_COLOR.' and key_id='.$md_id.' and merch_id='.$user['user_id'],
							'order'=>'file_id asc'
					));
				}else{
					$merchs['recomm_merch']=$file_mod->find(array(
							'conditions'=>'file_type='.BELONGS_SAMPLE_RECOMM_COLOR.' and key_id='.$md_id,
							'order'=>'file_id asc'
					));
				}
				$this->assign('belongs_ref_img',BELONGS_SAMPLE_REF_IMG);
				$this->assign('belongs_recomm_color',BELONGS_SAMPLE_RECOMM_COLOR);
			}
			$this->assign('merch',$merchs);
			$this->assign('user',$user);
			$this->display('merchant.fabric.html');
		}else{
			$sub_value=trim($_POST['sub_val']);
			$data=array();
			if ($sub_value=="审批通过"){
				if (in_array($user['role'], array(USER_MANAGER,USER_MERCHANDISER_ADMIN))){
					if ($merchs['demand_status']=1){
						$data['demand_status']=2;
					}
				}
			}
			$data['demand_descrption']=$_POST['demand_descrption'];
			$this->_merch_mod->edit($md_id,$data);
			if ($this->_merch_mod->has_error()){
				$this->show_warning($this->_merch_mod->get_error());
				return;
			}
			$new_demand=$this->_merch_mod->get_info($md_id);
			$member_mod=&m('member');
			if ($new_demand['demand_status']==2){
				if ($new_demand['order_id']){
					$belongs_img=BELONGS_ORDER_REF_IMG;
					$orders=$order_mod->get(array('conditions'=>'order_id='.$new_demand['order_id'],'fields'=>'order_sn'));
					$new_demand['sn']=$orders['order_sn'];
				}
				if ($new_demand['sample_id']){
					$belongs_img=BELONGS_SAMPLE_REF_IMG;
					$samples=$sample_mod->get(array('conditions'=>'sample_id='.$new_demand['sample_id'],'fields'=>'sample_sn'));
					$new_demand['sn']=$samples['sample_sn'];
				}
				$file_mod=&m('upload');
				$new_demand['ref_merch']=$file_mod->find(array(
						'conditions'=>'file_type='.$belongs_img.' and key_id='.$md_id,
						'order'=>'file_id asc'
				));
				$mail = get_mail('user_on_send_merchant', array('demand' => $new_demand));
				$this->_mailto(DEFAULT_MERCH_SEND_EMAIL, addslashes($mail['subject']), addslashes($mail['message']));
				//$this->_sendmail ( true ); // 即时发送
			}
			
			$this->show_message('提交成功');
		}
	}
	
	//添加
	function add(){
		$fs_mod=&m('fabric_shop');
		$user_mod=&m('member');
		if (!IS_POST){
			$this->assign('fabric_types', $this->_get_options());
			$this->display('merchant.form.html');
			$this->assign('user',$this->visitor->get());
			$this->_curlocal(Lang::get('merchant_info'));
		}else{
			$data=array();
			$merch_data=array();
			$fabric_name=trim($_POST['fabric_shop_name']);
			$email=trim($_POST['email']);
			$data['fabric_shop_name']=$fabric_name;
			$data['type_id']=$_POST['type_id'];
			$data['description']=$_POST['description'];
			$data['if_show']=1;
			$merch_data['user_name']=$fabric_name;
			$merch_data['role']=USER_MERCHANT;
			$merch_data['password']=md5(DEFAULT_MERCH_PASSWORD);
			$merch_data['reg_time']=gmtime();
			$merch_data['activation']=0;
			$merch_data['phone_tel']=trim($_POST['phone_tel']);
			$merch_data['phone_mob']=trim($_POST['phone_mob']);
			$merch_data['address']=trim($_POST['address']);
			$merch_data['email']=$email;
			$merch_data['is_qq']=trim($_POST['is_qq']);
			$merch_data['wx_sn']=trim($_POST['wx_sn']);
			$merch_data['ftype_id']=trim($_POST['type_id']);
			if (empty($fabric_name)) {
				$this->show_warning('fabric_shop_name_not_empty');
				return;
			}
			if (!$fs_mod->unique($fabric_name)) {
				$this->show_warning('fabric_shop_is_exist');
				return;
			}
			if ($email){
				if (!is_email($email)) {
					$this->show_warning('email_error');
					return;
				}
			}
			
			$user_id=$user_mod->add($merch_data);
			if ($user_id){
				$data['user_id']=$user_id;
			}
			if ($user_mod->has_error()){
				$this->show_warning($user_mod->get_error());
				return;
			}
			$fs_mod->add($data);
			if ($fs_mod->has_error()){
				$this->show_warning($fs_mod->get_error());
				return;
			}
			$this->show_message('布行/档口添加成功!',Lang::get('add_merch'),'index.php?app=colorcard');
		}
	}
	
	
	//编辑
	function edit(){
		$uid=!empty($_GET['uid'])?$_GET['uid']:0;
		$user_info=$this->visitor->get();
		if (!$uid){
			$this->show_warning("该供应商不存在");
			return;
		}
		$fs_mod=&m('fabric_shop');
		$user_mod=&m('member');
		if (!IS_POST){
			$merch=$fs_mod->get_merch_info($uid);
			$this->assign('fabric_types', $this->_get_options());
			$this->assign('merch',$merch);
			$this->assign('user_id',$uid);
			$this->assign('user',$user_info);
			$this->display('member.merch.html');
			/* 当前位置 */
			if ($user_info['role']==USER_ADMIN){
				$this->_curlocal(LANG::get('member_manage'), 'index.php?app=member&act=user_list&utype=merchant', Lang::get('edit_merch_info'));
			}else{
				$this->_curlocal(LANG::get('member_manage'), 'index.php?app=fabric_shop&act=activation', Lang::get('edit_merch_info'));
			}
		}else{
			$data=array();
			$merch_data=array();
			$user_name=trim($_POST['user_name']);
			$fabric_name=trim($_POST['fabric_shop_name']);
			$email=trim($_POST['email']);
			$data['fabric_shop_name']=$fabric_name;
			$data['type_id']=$_POST['type_id'];
			$data['description']=$_POST['description'];
			$data['if_show']=$_POST['if_show'];
			$merch_data['phone_tel']=trim($_POST['phone_tel']);
			$merch_data['phone_mob']=trim($_POST['phone_mob']);
			$merch_data['address']=trim($_POST['address']);
			$merch_data['email']=$email;
			$merch_data['is_qq']=trim($_POST['is_qq']);
			$merch_data['user_name']=$user_name;
			$merch_data['wx_sn']=trim($_POST['wx_sn']);
			$merch_data['ftype_id']=trim($_POST['type_id']);
			if (empty($fabric_name)) {
				$this->show_warning('fabric_shop_name_not_empty');
				return;
			}
			if (!$user_mod->unique($user_name,$uid)) {
				$this->show_warning('该用户名已存在');
				return;
			}
			if (!$fs_mod->unique($fabric_name,null,$uid)) {
				$this->show_warning('fabric_shop_is_exist');
				return;
			}
			if ($email){
				if (!is_email($email)) {
					$this->show_warning('email_error');
					return;
				}
			}
			if (!empty($_POST['password']))
			{
				$password = trim($_POST['password']);
				if (strlen($password) < 6 || strlen($password) > 20)
				{
					$this->show_warning('password_length_error');
					return;
				}
			}
			!empty($_POST['password']) && $merch_data['password'] = md5(trim($_POST['password']));
				
			$user_mod->edit($uid,$merch_data);
			if ($user_mod->has_error()){
				$this->show_warning($user_mod->get_error());
				return;
			}
			$fs_mod->edit('user_id='.$uid,$data);
			if ($fs_mod->has_error()){
				$this->show_warning($fs_mod->get_error());
				return;
			}
			if ($user_info['role']==USER_ADMIN){
				$this->show_message('供应商编辑成功!');
			}else{
				$this->show_message('供应商编辑成功!');
			}
		}
	}
	
	//删除
	function drop(){
		$md_id=empty($_GET['md_id'])?0:intval($_GET['md_id']);
		if (!$md_id) {
			$this->show_warning('demand_no_exist');
			return;
		}
		
		$this->_merch_mod->drop($md_id);
		if ($this->_merch_mod->get_error()) {
			$this->_error($this->_merch_mod->get_error());
			return;
		}
		
		$this->show_message('删除成功', Lang::get('demand_list'), 'index.php?app=merchant');
	}
	
	//结束
	function stop_demand(){
		$md_id=empty($_GET['md_id'])?0:intval($_GET['md_id']);
		if (!$md_id) {
			$this->show_warning('demand_no_exist');
			return;
		}
		
		$this->_merch_mod->edit($md_id,array('demand_status'=>3));
		if ($this->_merch_mod->get_error()) {
			$this->_error($this->_merch_mod->get_error());
			return;
		}
		$this->show_message('结束成功', Lang::get('demand_list'), 'index.php?app=merchant');
	}
	
	//历史需求
	function history_demand(){
		$user_info=$this->visitor->get();
		$page = $this->_get_page(10);
		$conditions=$this->_get_query_conditions(array(
				array(
						'field' => 'demand_sn',
						'equal' => 'like',
						'name'	 => 'demand_sn'
				)
		));
		if ($user_info['role']==USER_MERCHANDISER_ADMIN){
			$all_user=$this->child_user();
			$conditions.=" and user_id".db_create_in($all_user);
		}
		if ($user_info['role']==USER_MERCHANT){
			$conditions.=" and demand_status=3 and FIND_IN_SET({$user_info['user_id']},assign_merchant_id)";
		}elseif (in_array($user_info['role'], array(USER_MANAGER,USER_MERCHANDISER_ADMIN))){
			$conditions.=" and demand_status=3";
		}else{
			$conditions.=" and user_id={$user_info['user_id']} and demand_status=3";
		}
		
		$order = empty($_GET['order']) ? 'demand_time DESC' : str_replace('-', ' ', trim($_GET['order']));
		if ($order != 'demand_time DESC' || $order != 'demand_time ASC') {
			$order .= ', demand_time DESC';
		}
		$merch_list=$this->_merch_mod->find(array(
				'conditions'=>'1=1 '.$conditions,
				'order'=>$order,
				'limit'=>$page['limit'],
				'count'=>true
		));
		if (!empty($merch_list)){
			$file_mod=&m('upload');
			foreach ($merch_list as $key=>$val){
				$files=$file_mod->get(array(
						'conditions'=>'file_type'.db_create_in(array(BELONGS_SAMPLE_REF_IMG,BELONGS_ORDER_REF_IMG)).' and key_id='.$val['md_id']
				));
				$merch_list[$key]['merch_img']=$files['file_path'];
			}
		}
		$this->_format_page($page);
		$this->assign('page_info', $page);
		$this->assign('merch_list',$merch_list);
		$this->assign('user',$user_info);
		$this->display('merchant.index.html');
		$this->_curlocal(Lang::get('history_demand'));
	}
	
	//重置
	function start_demand(){
		$md_id=empty($_GET['md_id'])?0:intval($_GET['md_id']);
		if (!$md_id) {
			$this->show_warning('demand_no_exist');
			return;
		}
	
		$this->_merch_mod->edit($md_id,array('demand_status'=>1));
		if ($this->_merch_mod->get_error()) {
			$this->_error($this->_merch_mod->get_error());
			return;
		}
		$this->show_message('重置成功', Lang::get('demand_list'), 'index.php?app=merchant&act=history_demand');
	}
	
	
	//分配供应商
	function assign_obj(){
		$user_info=$this->visitor->get();
		$md_id=empty($_GET['md_id'])?0:intval($_GET['md_id']);
		if (!$md_id) {
			$this->show_warning('demand_no_create');
			return;
		}
		if (in_array($user_info['role'], array(USER_MERCHANDISER,USER_MANAGER,USER_MERCHANDISER_ADMIN,USER_CLIENT,USER_CIVILIAN))){
			$assign_arr=array();
			$merchs=$this->_merch_mod->get($md_id);
			if (empty($merchs)) {
				$this->show_warning('demand_no_create');
				return;
			}
			if (!IS_POST){
				$fabric_type_mod=&m('fabric_type');
				$fabric_shop_mod=&m('fabric_shop');
				$assign_arr=$fabric_type_mod->find(array(
					'conditions'=>'parent_id=0 and if_show=1',
					'fields'=>'fabric_type_name'	
				));
				if (!empty($assign_arr)){
					foreach ($assign_arr as $key=>$val){
						$parent_id=$val['fabric_type_id'];
						$assign_arr[$key]['merch_user']=$fabric_shop_mod->find(array('conditions'=>'type_id='.$parent_id,'fields'=>'fabric_shop_name,user_id'));
						$child_arr=$fabric_type_mod->find(array('conditions'=>'if_show=1 and parent_id='.$parent_id,'fields'=>'fabric_type_name'));
						if (!empty($child_arr)){
							foreach ($child_arr as $k=>$v){
								$fid=$v['fabric_type_id'];
								$child_arr[$k]['last_merch']=$fabric_shop_mod->find(array('conditions'=>'type_id='.$fid,'fields'=>'fabric_shop_name,user_id'));
							}
						}
						$assign_arr[$key]['child_type']=$child_arr;
					}
				}
				$this->assign('all_merch',$assign_arr);
				$this->assign('assign_merchant',$merchs['assign_merchant_id']);
				$this->display('merchant.assign.html');
			}else{
				$data=array();
				$assign_merchant_id=$_POST['assign_merchant_id'];
				$merch_arr_id=implode(',', array_filter($assign_merchant_id));
				$data['assign_merchant_id']=$merch_arr_id;
				$this->_merch_mod->edit($md_id,$data);
				if ($this->_merch_mod->has_error()){
					$this->show_warning($this->_merch_mod->get_error());
					return;
				}
				$this->show_message('分配供应商成功');
			}
		}else{
			$this->show_warning('no_powers_to_visit');
			return;
		}
	}
	
	//获取跟单主管下的用户
	function child_user(){
		$user_arr=array();
		$mod_cm=&m("company_manager");
		$mod_brand=&m("brand");
		$brand_id=$mod_brand->find(array('conditions'=>"company_id in(select company_id from ($mod_cm->table) where uid={$this->visitor->get('user_id')})","fields"=>"brand_id"));
		$brands=array_keys($brand_id);
		$user_mod =& m('member');
		$buyer_list=$user_mod->find(array(
				'conditions'=>'role='.USER_CLIENT.' and activation=1'
		));
		$flag=0;
		if (!empty($buyer_list)){
			foreach ($buyer_list as $j=>$row){
				$brand_arr_id=explode(',', $row['brand_arr_id']);
				$brand_arr=array_merge($brand_arr_id,$brands);
				$brand_arr_ids=array_count_values($brand_arr);
				if (!empty($brand_arr_ids)){
					foreach ($brand_arr_ids as $i=>$r){
						if ($brand_arr_ids[$i]>1){
							$flag=1;
							break;
						}else{
							$flag=0;
						}
					}
				}
				if ($flag){
					$freight_id[]=$row['freight_id'];
				}
			}
		}
		if (!empty($freight_id)){
			$member_list=$user_mod->find(array(
				'conditions'=>'role='.USER_CLIENT.' and activation=1 and freight_id'.db_create_in($freight_id)	
			));
			if (!empty($member_list)){
				foreach ($member_list as $val){
					$member_arr[]=$val['user_id'];
				}
			}
		}
		$user_arr=array_merge($freight_id,$member_arr);
		return $user_arr;
	}
	
	
	/* 构造并返回树 */
	function &_tree($types)
	{
		import('tree.lib');
		$tree = new Tree();
		$tree->setTree($types, 'fabric_type_id', 'parent_id', 'fabric_type_name');
		return $tree;
	}
	
	/* 取得可以作为上级的类型数据 */
	function _get_options($except = NULL)
	{
		$ft_mod=&m('fabric_type');
		$types = $ft_mod->get_list();
		$tree =& $this->_tree($types);
		return $tree->getOptions(MAX_LAYER - 1, 0, $except);
	}
}